#include "Button.h"

Button::Button(std::string buttonName, std::string path, int posX, int posY,
		Widget *parent, Listener* listener) :
	Widget(posX, posY, parent) {

	this->pressed = false;
	this->buttonName = buttonName;
	//	this->pressedSurface = SDL_SetVideoMode(100, 30, 24, SDL_SWSURFACE);
	//	this->defaultSurface = SDL_SetVideoMode(100, 30, 24, SDL_SWSURFACE);

	std::stringstream pressedSurfacePath;
	pressedSurfacePath << "./trunk/res/img/" << path << "_pressed.png";
	std::stringstream defaultSurfacePath;
	defaultSurfacePath << "./trunk/res/img/" << path << "_default.png";

	this->pressedSurface = Widget::loadSurface(pressedSurfacePath.str());
	this->defaultSurface = Widget::loadSurface(defaultSurfacePath.str());

	this->parcel = getPosition();
	parcel.height = this->pressedSurface->h;// Es indistinto cualquiera de las
	parcel.width = this->pressedSurface->w;// dos superficies por que son iguales

	this->activeSurface = this->defaultSurface;// El boton activo es el no presionado

	this->listener = listener;

	SDL_Color textColor = { 255, 255, 255 };
	TTF_Font *font = NULL;
	font = TTF_OpenFont("./trunk/res/font/Neuton.ttf", 18);

	if (font == NULL) {
		Logger::log("Button", "createButtonSurfaces", "No se cargó la fuente");
	}

	if (this->buttonName != "") {
		char* c_buttonName = new char[this->buttonName.size() + 1];
		strcpy(c_buttonName, this->buttonName.c_str());
		SDL_Surface* textSurface = TTF_RenderText_Solid(font, c_buttonName,
				textColor);
		delete[] c_buttonName;

		SDL_Rect rect;

		rect.x = (this->parcel.width - textSurface->w) / 2;
		rect.y = (this->parcel.height - textSurface->h) / 2;
		rect.w = textSurface->w;
		rect.h = textSurface->h;

		SDL_BlitSurface(textSurface, NULL, this->defaultSurface, &rect);
		SDL_BlitSurface(textSurface, NULL, this->pressedSurface, &rect);

	}

	TTF_CloseFont(font);
	//	delete textSurface;


}

Button::~Button() {
	delete this->pressedSurface;
	delete this->defaultSurface;
}

void Button::handleEvent(Event event) {
	if (this->mouseOver(event)) {
		if ((event.getType()) == MouseButtonPressed) {
			this->pressed = true;
			Logger::log("Button", "handleEvent", "MOUSEBUTTONDOWN");
		} else if ((event.getType()) == MouseButtonReleased) {
			this->pressed = false;
			Logger::log("Button", "handleEvent", "SDL_MOUSEBUTTONUP");
		}
	}
}

void Button::resolve() {
	if (this->pressed) {
		this->activeSurface = this->pressedSurface;
		this->listener->execute();
	} else if (!this->pressed) {
		this->activeSurface = this->defaultSurface;
	}
}

void Button::showOn(SDL_Surface *surface) {
	SDL_Rect rect;
	rect.x = parcel.posX;
	rect.y = parcel.posY;
	rect.w = parcel.width;
	rect.h = parcel.height;
	SDL_BlitSurface(this->activeSurface, NULL, surface, &rect);
}

bool Button::mouseOver(Event event) const {
	return ((event.getXCoordinate() > this->parcel.posX)
			&& (event.getXCoordinate() < this->parcel.posX + this->parcel.width)
			&& (event.getYCoordinate() > this->parcel.posY)
			&& (event.getYCoordinate() < this->parcel.posY
					+ this->parcel.height));
}
